package team.sun.sss.controller.hardwork;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import team.sun.sss.dao.hardwork.*;
import team.sun.sss.dao.student.FamilyDao1;
import team.sun.sss.dao.student.StudentDao;
import team.sun.sss.pojo.hardwork.HwBatch;
import team.sun.sss.pojo.hardwork.HwLabor;
import team.sun.sss.pojo.student.Student1;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Controller
public class HwLaborController {
    @Autowired
    HwApplyDao hwApplyDao;
    @Autowired
    StudentDao studentDao;
    @Autowired
    FamilyDao1 familyDao1;
    @Autowired
    HwBatchDao hwBatchDao;
    @Autowired
    HwLaborDao hwLaborDao;
    @Autowired
    HwTimeDao hwTimeDao;
    @Autowired
    HwContentDao hwContentDao;
    @Autowired
    HwStatisticsDao hwStatisticsDao;
    //学生
    @RequestMapping("/hw_stu_acceptLabor")
    public String stuAcceptLabor(Model model ){
        model.addAttribute("HwLaborList3",hwLaborDao.stuHwLaborList());
        return "hardWork/hw_stu_acceptLabor";
    }
    //学工
    @RequestMapping("/hw_staff_unitApply")
    public String staffUnitApply(Model model,@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                 @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){
        if(pageNum == null){
            pageNum = 1;   //设置默认当前页
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 5;    //设置默认每页显示的数据数
        }
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<HwLabor> studentList = hwLaborDao.getHwLaborList();
            model.addAttribute("HwLaborList2",studentList);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>

            PageInfo<HwLabor> pageInfo = new PageInfo<HwLabor>(studentList,pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo",pageInfo);
        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        model.addAttribute("teamList",hwBatchDao.getTeamName());

        return "hardWork/hw_staff_unitApply";
    }
    @RequestMapping("/staff_unitApply_select")
    public String hw_unitApply_select(Model model,String batchTeam ,String laborUnit, String laborState,@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                      @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){
        model.addAttribute("teamList",hwBatchDao.getTeamName());
        model.addAttribute("HwLaborList2",hwLaborDao.selectLabor(batchTeam,laborUnit,laborState));
        return "hardWork/hw_staff_unitApply2";
    }

    @RequestMapping("/hw_staff_unitapply_dateail/{batchTeam}/{laborUnit}/{num}")
    public String staffUnitApplyDetail(Model model, @PathVariable("batchTeam") String batchTeam,
                                  @PathVariable("laborUnit") String laborUnit ,@PathVariable("num") Integer num  ){
        model.addAttribute("hwLaborinfo2",hwLaborDao.HwLaborDetaile(num));
        model.addAttribute("hwContentinfo2",hwContentDao.getHwContentList(laborUnit,batchTeam));
        return "hardWork/hw_staff_unitapply_dateail";

    }
    @RequestMapping("/to_hw_staff_unitapply_update/{batchTeam}/{laborUnit}/{num}")
    public String tostaffUnitApplyupdate(Model model, @PathVariable("batchTeam") String batchTeam,
                                       @PathVariable("laborUnit") String laborUnit ,@PathVariable("num") Integer num  ){
        model.addAttribute("hwLaborinfo3",hwLaborDao.HwLaborDetaile(num));
        model.addAttribute("hwContentinfo3",hwContentDao.getHwContentList(laborUnit,batchTeam));
        return "hardWork/hw_staff_unitapply_update";
    }
    @RequestMapping("/hw_staff_unitapply_update")
    public String staffUnitApplyupdate(Model model,Integer laborNum ,String batchTeam,String laborUnit,String laborApprovalState,
                                       Integer laborRecruitersNum){
        System.out.println(laborNum);
        System.out.println(laborApprovalState);
        hwLaborDao.updateHwLabor(laborNum,laborApprovalState);
        if(laborApprovalState.equals("审核通过")){
            hwStatisticsDao.addHwStatistics(batchTeam,laborUnit,laborRecruitersNum);
        }
        model.addAttribute("hwLaborList2",hwLaborDao.getHwLaborList());
        return "hardWork/hw_staff_unitApply";
    }
    @RequestMapping("/hw_unit_apply")
    public String employerApply(Model model){
        //用人单位申请表
        model.addAttribute("HwLaborList",hwLaborDao.getHwLaborList());
        return "hardWork/hw_unit_apply";
    }
    @RequestMapping("/to_hw_unit_writeApply")
    public String tounitApply(Model model){
        //用人单位申请表

        model.addAttribute("laborBatch",hwBatchDao.getTeamName());
        return "hardWork/hw_unit_applyWrite";
    }
    //填写用人单位申请
    @RequestMapping("/hw_unit_writeApply")
    public String employerWriteApply(Model model,
                                     String batchTeam,String laborUnit,int laborRequestNum,int laborRecruitersNum,
                                     String laborStandard,String laborReason,
                                     String content1,String time1,String month1,
                                     String content2,String time2,String month2,
                                     String content3,String time3,String month3,
                                     String content4,String time4,String month4,
                                     String content5,String time5,String month5){
        //用人单位申请表
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        String submitTime = today.format(fmt);
        String larborApprovalState="未审核";
        hwLaborDao.addHwLabor(batchTeam,laborUnit,laborRequestNum,laborRecruitersNum,laborStandard,laborReason,submitTime,larborApprovalState);
        if(content1!=null && time1!=null && month1!=null){
            hwContentDao.addHwContent(content1, time1, month1, laborUnit,batchTeam);
        }else if(content2!=null && time2!=null && month2!=null){
            hwContentDao.addHwContent(content2, time2, month2, laborUnit,batchTeam);
        }else if(content3!=null && time3!=null && month3!=null ){
            hwContentDao.addHwContent(content3, time3, month3, laborUnit,batchTeam);
        }else if(content4!=null && time4!=null && month4!=null){
            hwContentDao.addHwContent(content4, time4, month4, laborUnit,batchTeam);
        }else if(content5!=null && time5!=null && month5!=null ){
            hwContentDao.addHwContent(content5, time5, month5, laborUnit,batchTeam);
        }
        model.addAttribute("HwLaborList",hwLaborDao.getHwLaborList());
        return "hardWork/hw_unit_apply";
    }
    @RequestMapping("/hw_unit_apply_dateail/{batchTeam}/{laborUnit}/{num}")
    public String unitApplyDetail(Model model, @PathVariable("batchTeam") String batchTeam,
                                  @PathVariable("laborUnit") String laborUnit ,@PathVariable("num") Integer num    ){
        model.addAttribute("hwLaborinfo1",hwLaborDao.HwLaborDetaile(num));
        model.addAttribute("hwContentinfo1",hwContentDao.getHwContentList(laborUnit,batchTeam));
        return "hardWork/hw_unit_applyDateail";

    }



}
